<template>
    <div class="table-case">
      <table class="my-table">
        <thead>
          <tr>
            <slot name="head"></slot>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in data" :key="index" @dragstart="dragStart(index)" @drop="drop(index)" @dragover.prevent>
            <slot name="body" :item="item" :index="index"></slot>
          </tr>
        </tbody>
      </table>
    </div>
  </template>
  
  <script>
  export default {
    name: 'MyTable',
    props: {
      data: {
        type: Array,
        require: true
      }
    },
    data () {
      return {
        draggedIndex: 0,
        endIndex: 0
      }
    },
    methods: {
      dragStart (index) {
        this.draggedIndex = index
      },
      drop (index) {
        this.endIndex = index
        const obj = {
          start: this.draggedIndex,
          end: this.endIndex
        }
        this.$emit('swapThem', obj)
      }
    }
  }
  </script>
  
  <style lang="less" scoped>
  .table-case {
    width: 1000px;
    margin: 50px auto;
    img {
      width: 100px;
      height: 100px;
      object-fit: contain;
      vertical-align: middle;
    }
  
    .my-table {
      width: 100%;
      border-spacing: 0;
      tr {
        transition:  background-color .3s;
        &:hover {
          background-color: rgba(0,0,0,.4);
          color: #fff;
          cursor: pointer;
        }
      }
      img {
        width: 100px;
        height: 100px;
        object-fit: contain;
        vertical-align: middle;
      }
      th {
        background: #000;
        color:#fff;
        border-bottom: 2px solid #ccc;
      }
      td {
        border-bottom: 1px dashed #ccc;
      }
      td,
      th {
        text-align: center;
        padding: 10px;
        transition: all 0.5s;
        &.red {
          color: red;
        }
      }
      .none {
        height: 100px;
        line-height: 100px;
        color: #999;
      }
    }
  }
  </style>
  
  